package com.yoomiito.app.ui.songduoduo.freegoods

import android.text.Spannable
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StrikethroughSpan
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.model.bean.GoodsInfo
import com.yoomiito.app.utils.*
import com.yoomiito.app.utils.DataUtils.ivType
import com.yoomiito.app.widget.CouponView

class RecommendGoodsAdapter(data: List<GoodsInfo>?) : BaseQuickAdapter<GoodsInfo, BaseViewHolder>(R.layout.item_recommend_goods, data) {

    override fun convert(helper: BaseViewHolder?, item: GoodsInfo?) {
        helper?.getView<ImageView>(R.id.item_goods_img).let {
            ImageLoader.getInstance().into(mContext, item?.imageUrl, 4, it, R.drawable.goods_default)
        }
        helper?.getView<TextView>(R.id.goodsTitle).let {
            it?.text = ""
            val ivResId = item?.source?.let { it1 -> Integer.valueOf(it1) }
                ?.let { it2 -> ivType(it2) }
            it?.append(SpannableUtils.getIconAndText(item?.title,
                ivResId?.let { it1 -> ResUtil.getDrawable(it1) }))
        }

        //优惠券
        helper?.getView<CouponView>(R.id.couponView).let {
            if (SpannableUtils.isEmptyOr0(item?.couponPrice)) {
                it?.setHeaderTextVisible(false)
                it?.setText("特价精选")
            } else {
                it?.setHeaderTextVisible(true)
                it?.setText("¥${item?.couponPrice}")
            }
            it?.setVisibility(View.VISIBLE)
        }

        helper?.getView<TextView>(R.id.goodsSize)?.text = "月销${SpannableUtils.saleNum(item?.salesNum)}"
        //奖--佣金  会员以上显示  分享赚同奖
        helper?.getView<CouponView>(R.id.awardView).let {
            if (App.mAppConfig.isLogin) {
                it?.visibility = if (UserInfoConfig.getRoleId() < Type.LevelType.VIP_7 || SpannableUtils.isEmptyOr0(item?.buyAwardPrice)) View.GONE else View.VISIBLE
                it?.setText("¥" + item?.buyAwardPrice)
            } else {
                it?.setVisibility(View.GONE)
            }
        }

        val goodsPrice: String = item?.discountPrice!!
        val oldPrice: String = item?.zkFinalPrice!!
        val price = if (SpannableUtils.isEmptyOr0(item?.couponPrice)) "¥$goodsPrice " else "¥$goodsPrice ¥$oldPrice"
        val spannableString = SpannableString(price)
        spannableString.setSpan(RelativeSizeSpan(0.65f), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        spannableString.setSpan(RelativeSizeSpan(0.65f), (if (goodsPrice.contains(".")) goodsPrice.indexOf(".") else goodsPrice.length) + 1, price.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        spannableString.setSpan(ForegroundColorSpan(ResUtil.getColor(R.color.color_balk_999999)), goodsPrice.length + 2, price.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        val strikethroughSpan = StrikethroughSpan()
        spannableString.setSpan(strikethroughSpan, goodsPrice.length + 2, price.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        helper?.getView<TextView>(R.id.goodsPrice)?.text = (spannableString)

    }
}